package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //TODO 根据传入的分类pid查询分类信息
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryByParentId(@RequestParam("pid") Long pid) {
        List<Category> categories = this.categoryService.queryByParentId(pid);
        if (categories != null) {
            return ResponseEntity.ok(categories);
        }
        return ResponseEntity.status(204).build();
    }

    //TODO 编辑品牌时根据品牌的id查询对应分类的数据并回显
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid") Long bid) {
        List<Category> categories = this.categoryService.queryByBrandId(bid);
        if (categories != null && 0 != categories.size()) {
            return ResponseEntity.ok(categories);
        }
        return ResponseEntity.status(204).build();//204成功但是为找到值
    }

    @GetMapping("names")
    public ResponseEntity<List<String>> queryNameByIds(@RequestParam("ids") List<Long> ids) {
        List<String> names = this.categoryService.queryNameByIds(ids);
        if (names != null && 0 != names.size()) {
            return ResponseEntity.ok(names);
        }
        return ResponseEntity.status(204).build();
    }

    @GetMapping
    public ResponseEntity<List<Category>> queryCategoryByIds(@RequestParam("cids") List<Long> cids) {
        List<Category> categories = this.categoryService.queryCategoryByIds(cids);
        if (categories != null && 0 != categories.size()) {
            return ResponseEntity.ok(categories);
        }
        return ResponseEntity.status(204).build();
    }


    //TODO 根据cid3查询1-3级的分类
    @GetMapping("all/level")
    public ResponseEntity<List<Category>> queryAllByCid3(@RequestParam("id") Long id) {
        List<Category> list = this.categoryService.queryAllByCid3(id);
        if (list != null || 0 != list.size()) {
            return ResponseEntity.ok(list);
        }
        return ResponseEntity.status(204).build();
    }
}
